﻿@using Think9.Models;
@model Think9.Models.RuleMultipleEntity
@{ ViewBag.Title = "Add";
    Layout = "~/Areas/Shared/_LayuiForm.cshtml"; }

<form class="layui-form" lay-filter="formUser">
    <input style="display:none" name="RuleId" id="RuleId" value="@ViewBag.id">
    <input style="display:none" id="ValueScope" value="@Model.ValueScope">
    <div class="layui-form-item">
        <label class="layui-form-label required">名 称</label>
        <div class="layui-input-block">
            <input type="text" name="Name" id="Name" placeholder="必填 请输入名称" autocomplete="off" class="layui-input" lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">取值方式</label>
        <div class="layui-input-block">
            <input type="text" name="TbId" id="TbId" style="display:none">
            <input type="text" name="TbId_Exa" id="TbId_Exa" readonly="readonly" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">Value字段</label>
        <div class="layui-input-block">
            <select id="ValueFiled" name="ValueFiled" lay-verify="required">
                <option value="">==请选择==</option>
                @foreach (valueTextEntity item in ((List
<valueTextEntity>
      )ViewBag.SelectSourceList).FindAll(x => x.ClassID.Equals("valuetext")))
                {
                    <option value="@item.Value">@item.Text</option>
                }
            </select>
            <tip>Value字段存储在数据库中</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">Text字段</label>
        <div class="layui-input-block">
            <select id="TxtFiled" name="TxtFiled" lay-verify="required">
                <option value="">==请选择==</option>
                @foreach (valueTextEntity item in ((List
<valueTextEntity>
       )ViewBag.SelectSourceList).FindAll(x => x.ClassID.Equals("valuetext")))
                {

                    <option value="@item.Value">@item.Text</option>
                }
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-row">
            <div class="layui-col-md10">
                <label class="layui-form-label required">排序字段</label>
                <div class="layui-input-block">
                    <select id="OrderFiled" name="OrderFiled" lay-verify="required">
                        <option value="">==请选择==</option>
                        @foreach (valueTextEntity item in ((List
<valueTextEntity>
               )ViewBag.SelectSourceList).FindAll(x => x.ClassID.Equals("order")))
                        {

                            <option value="@item.Value">@item.Text</option>
                        }
                    </select>
                </div>
            </div>
            <div class="layui-col-md2" align="left">
                <div class="layui-input-block">
                    <input type="hidden" name="OrderType" value="1">
                    <input type="checkbox" id="OrderType" name="OrderType" value="2" title="倒序">
                </div>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">查询条件</label>
        <div class="layui-input-block">
            <input type="hidden" name="RuleBy" id="RuleBy">
            <input type="text" name="RuleBy_Exa" id="RuleBy_Exa" placeholder="点击选择" autocomplete="off" class="layui-input">
            <span style="position: absolute;bottom: 20px; right: 5px;">
                <i class="fa fa-edit" id="selRuleBy"></i>
            </span>
            <tip>弹出页面时，可使用设定的查询条件进行数据筛选查询</tip>
        </div>
    </div>
    <fieldset class="table-fieldset">
        <legend>显示列表 最多不超过10列</legend>
        <div class="layui-form-item">
            <div class="layui-row">
                <div class="layui-col-md10">
                    <label class="layui-form-label">列表字段</label>
                    <div class="layui-input-block">
                        <select id="ListFiled" name="ListFiled">
                            @foreach (valueTextEntity item in ((List
<valueTextEntity>
                   )ViewBag.SelectSourceList).FindAll(x => x.ClassID.Equals("valuetext")))
                            {

                                <option value="@item.Value">@item.Text</option>
                            }
                        </select>
                    </div>
                </div>
                <div class="layui-col-md2" align="left">
                    <div class="layui-input-block">
                        <button type="button" class="layui-btn " id="addListFiled"><i class="fa fa-plus"></i></button>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
        </div>
    </fieldset>

    <fieldset class="table-fieldset">
        <legend>筛选条件</legend>
        <div class="layui-form-item">
            <label class="layui-form-label">连 接</label>
            <div class="layui-input-inline">
                <select name="linkstr" id="linkstr">
                    <option value=""></option>
                    <option value=" AND "> 并且 ( AND )</option>
                    <option value=" OR "> 或者 ( OR )</option>
                </select>
            </div>
            <label class="layui-form-label">判 断</label>
            <div class="layui-input-inline">
                <select name="judge" id="judge">
                    <option value="=">等于</option>
                    <option value=">">大于</option>
                    <option value=">=">大于等于</option>
                    <option value="<">小于</option>
                    <option value="<=">小于等于</option>
                    <option value="<>">不等于</option>
                    <option value="like">包含</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">字 段</label>
            <div class="layui-input-block">
                <select id="ConditionFiled" name="ConditionFiled">
                    @foreach (valueTextEntity item in ((List
<valueTextEntity>
)ViewBag.SelectSourceList).FindAll(x => x.ClassID.Equals("condition")))
                    {

                        <option value="@item.Value">@item.Text</option>
                    }
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">值</label>
            <div class="layui-input-inline">
                <input type="text" name="valuekey" id="valuekey" placeholder="输入或选择" class="layui-input">
            </div>
            <div class="layui-inline">
                <button type="button" class="layui-btn layui-btn-primary" id="selectvalue"><i class="fa fa-check"></i>选择系统参数</button>
            </div>
            <div class="layui-inline">
                <button type="button" class="layui-btn " id="addvalue"><i class="fa fa-plus"></i>添加筛选条件</button>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">筛选条件</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" id="LimitStr" name="LimitStr" placeholder='可自定义SQL语句，如字段1=条件1，其中条件可使用系统参数，如字段1=@@sysuserid，其中@@sysuserid表示用户登陆名'></textarea>
            </div>
        </div>
    </fieldset>

    <br />
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="edit">编辑</button>
        </div>
    </div>
</form>
<script>
    layui.use(["table", "form", "exLayer", "exUtils", "layer"], function () {
        let table = layui.table;
        let form = layui.form;
        let exLayer = layui.exLayer;
        let exUtils = layui.exUtils;
        let tableSelect = layui.tableSelect;
        let layer = layui.layer;

        let $ = layui.$;

        form.render();
        getVal();

        //给表单赋值
        form.val("formUser", {
            "Name": "@Model.Name",
            "TbId_Exa": "@Model.TbId_Exa",
            "TbId": "@Model.TbId",
            "ValueFiled": "@Model.ValueFiled",
            "TxtFiled": "@Model.TxtFiled",
            "OrderFiled": "@Model.OrderFiled",
            "RuleBy": "@Model.RuleBy",
            "RuleBy_Exa": "@Model.RuleBy_Exa",
            "LimitStr": "@Html.Raw(@Model.LimitStr)"
        });

        function getVal() {
            if (@Model.OrderType == "2") {
                $('#OrderType').attr("checked", true);
            }
            else {
                $("#OrderType").removeAttr("checked");
            }
        }

        let ThisTable = table.render({
            elem: "#tableId",
            url: "/SysTable/RuleMultiple/GetListFiled?id=" + $("#RuleId").val(),
            page: false,
            cols: [[
                { field: "FiledValue", title: "指标编码", width: 150 },
                { field: "FiledText", title: "指标名称" },
                { title: "操作", width: 120, align: "center", fixed: "right", templet: "#operationTpl" }
            ]],
            done: function (res, curr, count) {
                console.log(res, curr, count);
            }
        });

        table.on("tool(tableFilter)", function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case "del":
                    del(data.FiledValue);
                    break;
            }
        });

        form.on("submit(edit)", function (data) {
            exUtils.ajax("/SysTable/RuleMultiple/Edit", "post", { model: data.field, id: $("#Guid").val() }, true).done(function (response) {
                exLayer.greenTickMsg(response.message, function () {
                    $(window.parent.document).find('#search').click();
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                });
            }).fail(function (error) {
                console.log(error);
            });
            return false;
        });

        $('#selectvalue').on('click', function () {
            exLayer.openMiddle('选择', '/SysTable/RuleList/PUParameter', '800px', '400px', null, null);
        });

        $('#RuleBy_Exa').on('click', function () {
            var _tbid = $("#TbId").val();
            if (_tbid == '') {
                exLayer.yellowSighMsg("请先选择取值方式");
                return false;
            }
            else {
                exLayer.openMiddle("选择查询条件", "/SysTable/RuleList/PUSearchIndex?id=" + _tbid.replace(/#table#/, ""), '800px', '400px', null, null);
            }
        });

        $('#selRuleBy').on('click', function () {
            var _tbid = $("#TbId").val();
            if (_tbid == '') {
                exLayer.yellowSighMsg("请先选择取值方式");
                return false;
            }
            else {
                exLayer.openMiddle("选择查询条件", "/SysTable/RuleList/PUSearchIndex?id=" + _tbid.replace(/#table#/, ""), '800px', '400px', null, null);
            }
        });

        $('#addListFiled').on('click', function () {
            var _tbid = $("#TbId").val();
            var _id = $("#RuleId").val();
            var _value = $("#ListFiled").val();
            var _text = $("#ListFiled").find("option:selected").text();

            if (_value == '') {
                exLayer.yellowSighMsg("列表字段无效");
                return false;
            }
            else {
                exUtils.ajax("/SysTable/RuleMultiple/AddListFiled", "post", { id: _id, value: _value, text: _text, tbid: _tbid}, true).done(function (response) {
                    exUtils.tableSuccessMsg(response.message);
                    ThisTable.reload({
                        url: "/SysTable/RuleMultiple/GetListFiled?id=" + _id
                    });
                }).fail(function (error) {
                    console.log(error);
                });
            }
        });

        $('#addvalue').on('click', function () {
            var _old = $("#LimitStr").val();
            var _new = _old + ' ' + $("#linkstr").val() + ' ' + $("#ConditionFiled").val() + ' ' + $("#judge").val() + ' ' + $("#valuekey").val();
            $("#LimitStr").val(_new);

            $("#valuekey").val('');
        });

        function del(id) {
            exUtils.ajax("/SysTable/RuleMultiple/DeleteFiled", "get", {  rid: $("#RuleId").val(), indexid: id}, true).done(function (response) {
                ThisTable.reload({
                    url: "/SysTable/RuleMultiple/GetListFiled?id=" + $("#RuleId").val()
                });
            }).fail(function (error) {
                console.log(error);
            });
        }
    });
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del" id="del">删除</a>
</script>